package com.icss.daos;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.icss.beans.Consultation;
import com.icss.utils.DB;
import com.icss.utils.RecordSet;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-23 下午02:54:35
 * 类说明 咨询留言DAO
 */
public class ConsultationDao {
	
	private Dao dao=null;
	private ResultSet rSet=null;

	public ConsultationDao(){
		
	}
	/**
	 * 功能：添加咨询留言
	 * @param consultation
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public boolean addConsultation(Consultation consultation){
		dao=new Dao();
		StringBuffer sqlBuffer=new StringBuffer();
		sqlBuffer.append("insert into consultation(question,userName,questionTime,email) values(");
		sqlBuffer.append("'").append(consultation.getQuestion()).append("','")
			.append(consultation.getUserName()).append("','")
			.append(consultation.getQuestionTime().toLocaleString())
			.append("','").append(consultation.getEmail()).append("')");
		boolean b=dao.execute(sqlBuffer.toString());
		dao.close();
		return b;
	}
	
	/**
	 * 功能：删除一条留言
	 * @param consultationID
	 * @return
	 */
	public boolean delConsultation(int consultationID){
		dao=new Dao();
		StringBuffer sqlBuffer=new StringBuffer();
		sqlBuffer.append("delete from consultation where consultationID=").append(consultationID);
		boolean b=dao.execute(sqlBuffer.toString());
		dao.close();
		return b;
	}
	
	
	public Consultation queryForConsultation(int consultationID){
		return null;
	}
	
	/**
	 * 功能：分页查询
	 * @param currentPage
	 * @param pageSize
	 * @param condition
	 * @return
	 */
	public List<Consultation> queryConsultationsForPage(){
		List<Consultation> consultationList = null;		
		Consultation consultation=null;			
		consultationList=new ArrayList<Consultation>();
		ResultSet rSet = new Dao().query("select * from Consultation");
		try {
			while(rSet.next()){
				consultation=new Consultation();
				consultation.setConsultationID(rSet.getInt("consultationID"));
				consultation.setUserName(rSet.getString("userName"));
				consultation.setEmail(rSet.getString("email"));
				consultation.setQuestion(rSet.getString("question"));
				consultation.setQuestionTime(new Date(rSet.getDate("questionTime").getTime()));
				consultation.setVisiable(rSet.getInt("visiable"));
				consultation.setReply(rSet.getString("reply"));
				consultationList.add(consultation);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return consultationList;
	}
	
	/**
	 * 功能：更新留言
	 * @param consultationID
	 * @param visiable
	 * @return
	 */
	public boolean updateConsultation(int consultationID,int visiable){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("update consultation set visiable='").append(visiable)
			.append("' where consultationID=").append(consultationID);
		return dao.execute(sql.toString());
	}
	
	/**
	 * 功能：回复留言
	 * @param consultationID
	 * @param replyText
	 * @return
	 */
	public boolean updateConsultation(int consultationID,String replyText){
		dao=new Dao();
		StringBuffer sql=new StringBuffer();
		sql.append("update consultation set reply='").append(replyText)
			.append("' where consultationID=").append(consultationID);
		return dao.execute(sql.toString());
	}
}
